<?php
/**
 * Created by PhpStorm.
 * User: jin
 * Date: 2015/8/14
 * Time: 14:25
 */
namespace block;
class Curl
{
    static $httpCode = 0;
    static private $curl = '';

    static private function curlStart($url)
    {
        self::$curl = curl_init();
        curl_setopt(self::$curl,CURLOPT_URL,$url);
        curl_setopt(self::$curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt(self::$curl, CURLOPT_TIMEOUT,2);   //只需要设置一个秒的数量就可以
    }
    static private function curlEnd()
    {
        $OutPut = curl_exec(self::$curl);
        self::$httpCode = curl_getinfo(self::$curl,CURLINFO_HTTP_CODE);
        curl_close(self::$curl);
        return $OutPut;
    }

    /**
     * 发送GET请求
     * @param string $url
     * @return string mixed
     */
    static private function HttpGet($url)
    {
        self::curlStart($url);
        $OutPut = self::curlEnd();
        return $OutPut;
    }

    /**
     * 发送POST请求
     * @param string $url
     * @param array $data
     * @return string mixed
     */
    static private function HttpPost($url,$data)
    {
        self::curlStart($url);
        if(is_array($data)){
            $data=http_build_query($data);//转换成键值对的形�?
        }
        $cookie_file = "./google.txt"; //cookie存放地址
        //设置时区
        date_default_timezone_set('PRC');
        curl_setopt(self::$curl,CURLOPT_COOKIESESSION,1);
        curl_setopt(self::$curl,CURLOPT_COOKIEFILE,$cookie_file);
        curl_setopt(self::$curl,CURLOPT_COOKIEJAR,$cookie_file);
        curl_setopt(self::$curl,CURLOPT_COOKIE,session_name() . '=' . session_id());
        curl_setopt(self::$curl,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt(self::$curl,CURLOPT_POST,1);
        curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);//要发送的数据
        curl_setopt(self::$curl,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8",
            "Content-length:".strlen($data)));//http�?
        return self::curlEnd();
    }
    /**
     * 发送HTTPS的GET请求
     * @param string $url
     * @return string mixed
     */
    static private function HttpsGet($url){
        self::curlStart($url);
        date_default_timezone_set('PRC');
        curl_setopt(self::$curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt(self::$curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        return self::curlEnd();
    }
    /**
     * 发送HTTPS的POST请求
     * @param string $url
     * @param array $data
     * @return string mixed
     */
    static private function HttpsPost($url,$data){
        self::curlStart($url);
        if(is_array($data)){
            $data=http_build_query($data);//转换成键值对的形式
        }
        $cookie_file = "./google.txt"; //存放cookie的的形式
        //设置时区
        date_default_timezone_set('PRC');
        curl_setopt(self::$curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt(self::$curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt(self::$curl,CURLOPT_COOKIESESSION,1);
        curl_setopt(self::$curl,CURLOPT_COOKIEFILE,$cookie_file);
        curl_setopt(self::$curl,CURLOPT_COOKIEJAR,$cookie_file);
        curl_setopt(self::$curl,CURLOPT_COOKIE,session_name() . '=' . session_id());
        curl_setopt(self::$curl,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt(self::$curl,CURLOPT_POST,1);
        curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);//要发送的数据
        curl_setopt(self::$curl,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8",
            "Content-length:".strlen($data)));//https协议头
        return self::curlEnd();
    }
    static function Get($url){
        if(0 === strpos($url,'https')){
            return self::HttpsGet($url);
        }else{
            return self::HttpGet($url);
        }
    }
    static function Post($url,$data){
        if(0 === strpos($url,'https')){
            return self::HttpsPost($url,$data);
        }else{
            return self::HttpPost($url,$data);
        }
    }
}